//
//  NLoginViewController.m
//  nncloud
//
//  Created by kabu on 2018/9/17.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NLoginViewController.h"
#import "NRegisterViewController.h"
@interface NLoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameTF;

@property (weak, nonatomic) IBOutlet UITextField *pwdTF;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (weak, nonatomic) IBOutlet UIButton *eyeBtn;

@property (nonatomic,copy)NSString *userType;


@end

@implementation NLoginViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = NColorFrom16RGB(0x23b7e5);
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.translucent = NO;
//    self.navigationController.navigationBar.barTintColor = NColorRGB(88, 181, 225);
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
//    self.navigationItem.leftBarButtonItem = nil;
    [self.navigationItem setItemWithTitle:@"登录" textColor:kWhiteColor fontSize:18 itemType:center];
    self.loginBtn.layer.cornerRadius = kCornerRadius;
    self.loginBtn.layer.masksToBounds = YES;
    
    ///用户类型   0个人   1企业   2政府（无注册功能）
    
    NSInteger ii = [[NUserModel shareInstance].userInfoType integerValue];
    
    
    if (ii==0||ii==1) {
        
        [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"注册" Image:nil selectedIMG:nil tartget:self action:@selector(reAction:)];
        
    }
}


-(void)reAction:(UIButton *)btn
{
    YYLog(@"注册");
    NRegisterViewController *reg = [[NRegisterViewController alloc]initWithNibName:@"NRegisterViewController" bundle:nil];
    [self.navigationController pushViewController:reg animated:YES];
    
}


- (IBAction)eyeClicked:(UIButton *)sender {
    
    //按钮反选
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"openeye"] forState:UIControlStateSelected];
        self.pwdTF.secureTextEntry = NO;
        
    }else{
        [sender setImage:[UIImage imageNamed:@"closeye"] forState:UIControlStateNormal];
        self.pwdTF.secureTextEntry = YES;
    }
    
}

//登录
- (IBAction)loginAction:(UIButton *)sender {
    
    if ((self.userNameTF.text.length>0)&&(self.pwdTF.text.length>0)) {
        [self loadDataWithName:self.userNameTF.text pwd:self.pwdTF.text];
    }else{
        [AFToast showText:@"请完整填写"];
    }
    
}

- (void)loadDataWithName:(NSString *)name pwd:(NSString *)pwd{
    
    //test   111111
    WeakStament(ws);
    NSString *userType = [[NSUserDefaults standardUserDefaults]objectForKey:kUserType];
    self.userType = userType;
    NSDictionary *dic = @{@"userInfoType":userType?userType:@"1",@"userInfoName":name,@"userInfoNamePwd":pwd};
    [SVProgressHUD show];
    [[NNRCNetRequest manager]getWithUrl:kLoginUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        NSInteger code = [result[@"code"]integerValue];
        NSString *msg =  result[@"msg"];
        if (code==0) {
            //存储用户id
            if (result[@"page"]) {
                [NUserModel shareInstance].userInfoId = result[@"page"][@"userInfoId"];
                [NUserModel shareInstance].userInfoCertificationStatus = result[@"page"][@"userInfoCertificationStatus"];
                [ws loginSucceedWithResponseObj:result[@"page"]];
                [SVProgressHUD dismiss];
            }
        }else{
            [AFToast showText:msg];
            [SVProgressHUD dismiss];
        }
    } failure:^(id failure) {
        [SVProgressHUD dismiss];
        YYLog(@"%@",failure);
    }];
}
//登录成功之后进行存储和标记
- (void)loginSucceedWithResponseObj:(NSDictionary *)obj{
    //存储类型
    [self markUserTypeWithType:obj[@"userInfoType"]];
    [NUserModel saveUserInfoWithDic:obj];
    [self dicToModel];
    [NUserModel shareInstance].isLogin = YES;
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
    ///appdelegate 中也放有这个
    
    [self loadUnReadCount];
    
}

///获取未读消息数目
- (void)loadUnReadCount
{
    
    NSDictionary *dict = @{@"messageUserId":[NUserModel shareInstance].userInfoId};
    [NNetworkClient getRequestWithBaseUrl:kUnReadCountUrl setParameters:dict success:^(id responseObject) {
        YYLog(@"%@",dict);
    } fail:^(NSError *error) {
        YYLog(@"%@",error);
    } delegater:nil];
    
}

//存储用户类型
- (void)markUserTypeWithType:(NSString *)type {
    [[NSUserDefaults standardUserDefaults]setObject:type forKey:kUserType];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

///将字典转入单例模型  方便整个项目调用
- (void)dicToModel {
    
    NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults]objectForKey:kSaveUserInfoKey];
    if (userInfo) {
        
        NUserModel *model = [NUserModel shareInstance];
        [model setValuesForKeysWithDictionary:userInfo];
        YYLog(@"%@,%@",model.userInfoNickname,[NUserModel shareInstance].userInfoId);
        
    }
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
    
}


@end
